<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>物流列表</title>
    <script type="text/javascript">
      var newLogisticsWin;
      var editLogisticsWin;
      var logisticsId;
      var logisticsDS;

      //处理供应商锁定
      function lockLogisticsFn(id){
        Ext.Ajax.request({
          url : '/logistics/logisticsLock',
          success : function (result, request) {
            logisticsDS.reload();
          },
          failure : function (result, request){
            Ext.MessageBox.show({
              title: '消息',
              msg: "通讯失败，请从新操作",
              buttons: Ext.MessageBox.OK,
              icon: Ext.MessageBox.WARNING
            });
          },
          method : 'GET',
          params : {
            id : id
          }
        });
      }
      
      Ext.onReady(function(){
        //物流设置---------------------------------------------------------
        //生成有关物流的Grid
        logisticsDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize + 15
          },
          proxy: new Ext.data.HttpProxy({
            url: '/logistics/getAllLogistics'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            },{
              name: 'sub_type'
            }, {
              name: 'name'
            }, {
              name: 'phone'
            }, {
              name: 'fax'
            }, {
              name: 'linkman'
            }, {
              name: 'email'
            }, {
              name: 'address'
            }, {
              name: 'description'
            },{
              name: 'editor',
              mapping:'editor',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            },{
              name: 'is_locked'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        var logisticsSM = new Ext.grid.CheckboxSelectionModel();
        var logisticsCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),logisticsSM,{
              header: 'id',
              dataIndex: 'id',
              width: 30
            }, {
              header: '编号',
              dataIndex: 'number',
              width:50
            }, {
              header: '类型',
              dataIndex: 'sub_type',
              width:45
            }, {
              header: '名称',
              dataIndex: 'name',
              width:180
            }, {
              header: '电话',
              dataIndex: 'phone',
              width:230
            }, {
              header: '传真',
              dataIndex: 'fax',
              width:150
            }, {
              header: '地址',
              dataIndex: 'address',
              width:260
            }, {
              header: '电子邮箱',
              dataIndex: 'email',
              width:180
            }, {
              header: '联系人',
              dataIndex: 'linkman',
              width:80
            }, {
              header: '描述',
              dataIndex: 'description',
              width:230
            }, {
              header: '添加者',
              dataIndex: 'editor',
              width:50
            }, {
              header: '是否锁定',
              dataIndex: 'is_locked',
              renderer: renderLogisticsIsLucked,
              width: 60
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 110
            }]});

        //锁定物流公司
        function renderLogisticsIsLucked(value, cellmeta, record, rowIndex, columnIndex, store){
          if (record.get("is_locked")=="1"){
            return "<a style=cursor:pointer onclick=lockLogisticsFn(" + store.getAt(rowIndex).get('id')+")><font color=red>锁定</font></a>";
          }else{
            return "<a style=cursor:pointer onclick=lockLogisticsFn(" + store.getAt(rowIndex).get('id')+")><font color=green>未锁定</font></a>";
          }
        }
        
        var logisticsGrid =  new Ext.grid.GridPanel({
          ds: logisticsDS,
          cm: logisticsCM,
          sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
          width: screenWidth-175,
          height: screenHeight-275,
          iconCls: 'icon-grid',
          loadMask: true,
          //collapsible: true,
          //animCollapse: false,
          //frame: true,
          //title: '物流列表',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize + 15,
            store: logisticsDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: [{
              text: '添加',
              iconCls: 'addItem',
              handler : function(){
                newLogisticsWin = new Ext.Window({
                  layout: 'fit',
                  width:618,
                  height:300,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加物流',
                  autoLoad: {
                    url: "/logistics/newLogistics",
                    scripts: true
                  }
                });
                newLogisticsWin.on("destroy",function(){
                  logisticsDS.reload();
                });
                newLogisticsWin.resizable = false;
                newLogisticsWin.show();
              }
            },"-",{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = logisticsGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  //把表单添加到窗口中
                  logisticsId = records[0].get('id');
                  editLogisticsWin = new Ext.Window({
                    title: '编辑物流',
                    layout:'fit',
                    width:618,
                    height:300,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/logistics/editLogistics?id=" + logisticsId,
                      scripts: true
                    }
                  });
                }
                editLogisticsWin.on("destroy",function(){
                  logisticsDS.reload();
                });
                editLogisticsWin.resizable = false;
                editLogisticsWin.show();
              }
            }]
        });
        logisticsGrid.render('logistics_list');
        logisticsDS.load();
      })
    </script>
  </head>
  <body>
    <div id="logistics_list"></div>
  </body>
</html>
